package com.ycsj.common.manager;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.text.TextUtils;

import com.blankj.utilcode.util.LogUtils;
import com.ycsj.common.base.BaseApplication;

import java.io.IOException;

/**
 * 封装的MediaPlayer管理类
 * Created by Administrator on 2018/5/11 0011.
 */

public class MediaPlayerManager {

    private static MediaPlayerManager mInstance;
    private static MediaPlayer player;


    public MediaPlayer getPlayer() {
        return player;
    }

    public void setPlayer(MediaPlayer player) {
        this.player = player;
    }

    /**
     * 获取单例引用
     *
     * @return
     */
    public static MediaPlayerManager getInstance() {
        if (mInstance == null) {
            synchronized (MediaPlayerManager.class) {
                if (mInstance == null) {
                    mInstance = new MediaPlayerManager();
                }
            }
        }
        return mInstance;
    }

    /**
     * 播放录音
     *
     * @param url
     * @return
     */
    public boolean play(String url) {
        return play(url, new MediaPlayer.OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer arg0) {
                stop();
            }
        }, new MediaPlayer.OnErrorListener() {

            @Override
            public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
                stop();
                return false;
            }
        });
    }

    /**
     * 获取网络音频时长的方法
     */
    public static int getDuration(String url) {
        int res = 0;
        try {
            MediaPlayer player = new MediaPlayer();
            player.setDataSource(url);
            player.prepare();
            res = player.getDuration();
            player.release();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return res;
    }

    /**
     * 获取本地SD卡文件的音频时长的方法
     */
    public static int getSdCardDuration(Context context, String filePath) {
        int res = 0;
        try {
            MediaPlayer player = new MediaPlayer();
            player.setDataSource(context, Uri.parse(filePath));
            player.prepare();
            res = player.getDuration();
            player.release();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return res;
    }


    /**
     * 播放录音
     *
     * @param url
     * @return
     */
    public boolean play(String url, MediaPlayer.OnCompletionListener completionListener, MediaPlayer.OnErrorListener errorListener) {
        stop();
        if (TextUtils.isEmpty(url)) {
            return false;
        }
        try {
            if (player == null) {
                player = new MediaPlayer();
                player.setDataSource(url);
                player.setVolume(0.6f, 0.6f);
                player.setAudioStreamType(AudioManager.STREAM_MUSIC);
                player.prepare();
                player.start();
                player.setOnCompletionListener(completionListener);
                player.setOnErrorListener(errorListener);
                return true;
            }
        } catch (Exception e) {
            stop();
        }
        return false;
    }


    /**
     * 播放录音,带准备完成后的监听
     *
     * @param url
     * @param onPreparedListener
     * @param completionListener
     * @param errorListener
     * @return
     */
    public boolean play(String url, MediaPlayer.OnPreparedListener onPreparedListener, MediaPlayer.OnCompletionListener completionListener, MediaPlayer.OnErrorListener errorListener) {
        stop();
        if (TextUtils.isEmpty(url)) {
            return false;
        }
        try {
            if (player == null) {
                player = new MediaPlayer();
                player.setDataSource(url);
                player.setVolume(0.6f, 0.6f);
                player.setAudioStreamType(AudioManager.STREAM_MUSIC);
                player.prepare();
                player.setOnPreparedListener(onPreparedListener);
                player.start();
                player.setOnCompletionListener(completionListener);
                player.setOnErrorListener(errorListener);
                return true;
            }
        } catch (Exception e) {
            stop();
        }
        return false;
    }

    /**
     * 播放录音
     *
     * @return
     */
    public boolean playLocalFile(int resId, MediaPlayer.OnCompletionListener completionListener, MediaPlayer.OnErrorListener errorListener) {
        stop();
        if (resId == -1) {
            return false;
        }
        try {
            if (player == null) {
                player = new MediaPlayer();
                AssetFileDescriptor file = BaseApplication.context.getResources().openRawResourceFd(resId);

                try {
                    player.setDataSource(file.getFileDescriptor(), file.getStartOffset(),
                            file.getLength());
                    player.prepare();
                    file.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                player.setVolume(0.6f, 0.6f);
//                player.setLooping(true);
                player.setAudioStreamType(AudioManager.STREAM_MUSIC);
                player.start();
                player.setOnCompletionListener(completionListener);
                player.setOnErrorListener(errorListener);
                return true;
            }
        } catch (Exception e) {
            stop();
        }
        return false;
    }


    /**
     * 播放SD卡的文件
     *
     * @return
     */
    public boolean playSdCardFile(Context context, String filePath, MediaPlayer.OnPreparedListener onPreparedListener, MediaPlayer.OnCompletionListener completionListener, MediaPlayer.OnErrorListener errorListener) {
        stop();
        if (TextUtils.isEmpty(filePath)) {
            return false;
        }
        try {
            if (player == null) {
                player = new MediaPlayer();
                try {
                    player.setDataSource(context, Uri.parse(filePath));
                    player.prepare();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                player.setVolume(0.6f, 0.6f);
//                player.setLooping(true);
                player.setAudioStreamType(AudioManager.STREAM_MUSIC);
                player.start();
                player.setOnCompletionListener(completionListener);
                player.setOnPreparedListener(onPreparedListener);
                player.setOnErrorListener(errorListener);
                return true;
            }
        } catch (Exception e) {
            LogUtils.iTag("test", "播放报错:" + e);
            stop();
        }
        return false;
    }


    /**
     * 释放资源
     */
    public void stop() {
        if (player != null) {
            try {
                player.stop();
                player.release();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                player = null;
            }
        }
    }

}
